/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"FFFEEC93-97CD-4B69-B9B0-3F356AEDC73E"}
 */
var $adm_t900_usuarios_id = "";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"BED79949-3056-4AD5-B16E-C55B177014B9"}
 */
var $userName = "";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"A09AF5C6-1DF5-41A1-8F42-4DE870A6DF0A"}
 */
var $password = "";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"B8E0AC98-BC2F-42C0-B32B-421D098C5668"}
 */
var $errorMessage = '';

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"FCD09380-5560-4576-B844-C2FCF05F1B52"}
 */
var authSolution = "pigeon_authenticator";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"B4C26B91-C89B-449A-AB2F-6094636FE338"}
 */
var authMethod = "loginUser";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"4AD8D2A8-A771-4040-8F55-E6D34A0E3504"}
 */
var checkADM = "checkADM";

/**
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {JSEvent} event the event that triggered the action
 * @private
 *
 * @properties={typeid:24,uuid:"A262290C-5385-4E64-95BC-DBFAE5C86841"}
 */
function login(event) {

	$errorMessage = null;
	 
	
    if(!$userName){
        $errorMessage = "Especifique un nombre de usuario, por favor.";
        return false;
    }
    if(!$password){
        $errorMessage = "Especifique una contraseña, por favor.";
        return false;
    }
    
//    var tenantID = security.authenticate(authSolution, getTenant, [$userName]);
//    if (tenantID){
	if (true){
    	var adm_t900_usuarios_id  = security.authenticate(authSolution, authMethod, [$userName, $password]);
    	if (adm_t900_usuarios_id != null){
    		application.setUserProperty("adm_t900_usuarios_id", adm_t900_usuarios_id);
    		return true;
    	}else{
    		$userName = null;
    		$password = null;
        	$errorMessage = "Error en la autenticación";
        	controller.focusField(elements.fld_usuario.getName(), true);
        	application.sleep(3000);
    	}
    }
    return false;
}

/**
 * Callback method for when form is shown.
 *
 *@author Roberto J. Blasco
 *@version 1.0
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"9C9160DD-76DA-42EE-873D-C673190269D6"}
 */
function onShow(firstShow, event) {

	$errorMessage = "";
	$password = "";
	$userName = "";
	
	security.authenticate(authSolution,checkADM);
	
	elements.lbl_error.visible = false;
	controller.focusField(elements.fld_usuario.getName(),true);
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"285F09D6-5440-4F2C-8C97-2B951301741E"}
 */
function salir_aplicacion(event) {
	
	application.exit();
}
